﻿import 'package:flutter/material.dart';
import 'package:getx_scaffold/getx_scaffold.dart';
import 'audio_player_logic.dart';
import '../../../gen/assets.gen.dart';

/// 音频播放页面
class AudioPlayerView extends GetView<AudioPlayerLogic> {
  const AudioPlayerView({super.key});

  @override
  Widget build(BuildContext context) {
    // 初始化控制器
    Get.put(AudioPlayerLogic());

    return Scaffold(
      backgroundColor: Colors.black,
      body: Stack(
        children: [
          // 全屏背景图片
          Positioned.fill(
            child: controller.course?.fullImageUrl.isNotEmpty == true
                ? Image.network(
                    controller.course!.fullImageUrl,
                    fit: BoxFit.cover,
                    errorBuilder: (context, error, stackTrace) {
                      return Container(color: Colors.black);
                    },
                  )
                : Container(color: Colors.black),
          ),

          // 渐变遮罩
          Positioned.fill(
            child: Container(
              decoration: BoxDecoration(
                gradient: LinearGradient(
                  begin: Alignment.topCenter,
                  end: Alignment.bottomCenter,
                  colors: [
                    Colors.black.withOpacity(0.6),
                    Colors.black.withOpacity(0.8),
                  ],
                ),
              ),
            ),
          ),

          // 内容层
          SafeArea(
            child: Column(
              children: [
                // 顶部导航栏
                _buildAppBar(),

                // 中间空白区域
                const Spacer(),

                // 播放控制区域
                _buildPlayControls(),

                SizedBox(height: 40.h),
              ],
            ),
          ),
        ],
      ),
    );
  }

  /// 构建顶部导航栏
  Widget _buildAppBar() {
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 12.h),
      child: Row(
        children: [
          IconButton(
            icon: const Icon(Icons.arrow_back_ios, color: Colors.white),
            onPressed: () => Get.back(),
          ),
          Expanded(
            child: Text(
              controller.course?.actionName ?? '课程播放',
              style: TextStyle(
                fontSize: 18.sp,
                color: Colors.white,
                fontWeight: FontWeight.w500,
              ),
              textAlign: TextAlign.center,
            ),
          ),
          
        ],
      ),
    );
  }

  /// 构建播放控制区域
  Widget _buildPlayControls() {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 24.w),
      child: Column(
        children: [
          // 播放控制按钮行
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              // 后退15秒
              _buildControlButton(
                image: Assets.action.back15s,
                onPressed: controller.rewind,
              ),

              SizedBox(width: 40.w),

              // 播放/暂停按钮（中央椭圆按钮）
              Obx(() => GestureDetector(
                    onTap: () {
                      if (controller.state.isPlaying.value) {
                        controller.pause();
                      } else {
                        controller.play();
                      }
                    },
                    child: SizedBox(
                      width: 80.w,
                      height: 80.w,
                      child: Stack(
                        alignment: Alignment.center,
                        children: [
                          // 椭圆形背景
                          Assets.action.oval.image(
                            width: 80.w,
                            height: 80.w,
                            fit: BoxFit.contain,
                          ),
                          // 播放/暂停图标
                          controller.state.isPlaying.value
                              ? Assets.action.play.image(
                                  width: 40.w,
                                  height: 40.w,
                                  fit: BoxFit.contain,
                                )
                              : Assets.action.pause.image(
                                  width: 40.w,
                                  height: 40.w,
                                  fit: BoxFit.contain,
                                ),
                        ],
                      ),
                    ),
                  )),

              SizedBox(width: 40.w),

              // 前进15秒
              _buildControlButton(
                image: Assets.action.forward15s,
                onPressed: controller.fastForward,
              ),
            ],
          ),

          SizedBox(height: 40.h),

          // 进度条
          Obx(() => Column(
                children: [
                  SliderTheme(
                    data: SliderTheme.of(Get.context!).copyWith(
                      trackHeight: 4.h,
                      thumbShape: RoundSliderThumbShape(
                        enabledThumbRadius: 8.r,
                      ),
                      overlayShape: RoundSliderOverlayShape(
                        overlayRadius: 16.r,
                      ),
                      activeTrackColor: const Color(0xFFFF6B35),
                      inactiveTrackColor: Colors.grey[800],
                      thumbColor: const Color(0xFFFF6B35),
                      overlayColor: const Color(0xFFFF6B35).withOpacity(0.3),
                    ),
                    child: Slider(
                      value: controller.state.currentPosition.value,
                      max: controller.state.totalDuration.value > 0
                          ? controller.state.totalDuration.value
                          : 1.0,
                      onChanged: (value) {
                        controller.seekTo(value);
                      },
                    ),
                  ),

                  // 时间显示
                  Padding(
                    padding: EdgeInsets.symmetric(horizontal: 8.w),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text(
                          controller.formatDuration(
                              controller.state.currentPosition.value),
                          style: TextStyle(
                            fontSize: 12.sp,
                            color: Colors.white70,
                          ),
                        ),
                        Text(
                          controller.formatDuration(
                              controller.state.totalDuration.value),
                          style: TextStyle(
                            fontSize: 12.sp,
                            color: Colors.white70,
                          ),
                        ),
                      ],
                    ),
                  ),
                ],
              )),
        ],
      ),
    );
  }

  /// 构建控制按钮
  Widget _buildControlButton({
    required AssetGenImage image,
    required VoidCallback onPressed,
  }) {
    return GestureDetector(
      onTap: onPressed,
      child: SizedBox(
        width: 50.w,
        height: 50.w,
        child: image.image(
          width: 50.w,
          height: 50.w,
          fit: BoxFit.contain,
        ),
      ),
    );
  }
}
